package cn.xinzhi.dao.impl;

import cn.xinzhi.dao.IUserInfoDao;
import cn.xinzhi.entity.UserInfo;

import java.sql.*;

public class UserInfoDaoImpl implements IUserInfoDao {

    @Override
    public UserInfo login(String username, String password) {
        Connection conn = null;
        PreparedStatement pstat = null;
        ResultSet rs = null;
        UserInfo ui = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/xinzhidaliy","root","root");
            String sql = "select * from userinfo where username = ? and userpassword = ?";
            pstat = conn.prepareStatement(sql);
            pstat.setString(1, username);
            pstat.setString(2, password);
            rs = pstat.executeQuery();
            if (rs.next()) {
                ui = new UserInfo();
                ui.setPassword(rs.getString("password"));
                ui.setUsername(rs.getString("username"));
                ui.setUserid(rs.getInt("userid"));
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            try {
                if(rs != null)
                    rs.close();
                if(pstat != null)
                    pstat.close();
                if(conn != null)
                    conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return ui;
    }

}
